// 1.引入要用到的Api
import { defineStore } from "pinia"
import {reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData,} from "@/api/product/category"
import type {CategoryStore} from '@/api/product/model/categoryModel'
// 3. 创建模块对象
export const useCategoryListStore = defineStore('categoryList',{
  state:():CategoryStore=>{
    return {
      // 以下三个属性用于收集用户真正选中的分类的id
      category1Id:undefined,
      category2Id:undefined,
      category3Id:undefined,

      // 以下三个数组用户存储服务器响应回来的分类数据
      category1List:[],
      category2List:[],
      category3List:[],
    }
  },
  actions:{
    // 1. 获取一级分类数据
    async getCategory1ListData(){
      const result = await reqGetCategory1ListData()
      // console.log('pp',result);
      this.category1List = result
    },

    // 2. 获取二级分类数据
    async getCategory2ListData(category1Id:number){
      this.category1Id = category1Id
      this.category2List = await reqGetCategory2ListData(category1Id)
      // console.log('pp',result);

      // 重置二三级id
      this.category2Id = undefined
      this.category3Id = undefined
      
    },

    // 3. 获取三级分类数据
    async getCategory3ListData(category2Id:number){
      this.category2Id = category2Id
      this.category3List = await reqGetCategory3ListData(category2Id)

      // 重置三级id
      this.category3Id = undefined
    },

    // 4. 设置三级导航的值
    setCategory3Id(category3Id:number){
    this.category3Id = category3Id
    }
  },
  getters:{

  }
})